package macrotools.utils;

class StringBuilder 
{
	var buf:StringBuf;
	
	public function new(?str:Null<String>)
	{
		this.buf = new StringBuf();
		if (str != null)
			buf.add(str);
	}
	
	public function add(dyn:Dynamic):StringBuilder 
	{
		buf.add(dyn);
		return this;
	}
	
	public function addChar(chr:Int):StringBuilder 
	{
		buf.addChar(chr);
		return this;
	}
	
	public function addIf(dyn:Dynamic, cond:Bool):StringBuilder 
	{
		if (cond) buf.add(dyn);
		return this;
	}
	
	public function addMapJoin<T>(arr:Array<T>, fn:T->String, joinChr:String):StringBuilder 
	{
		var first = true;
		for (a in arr)
		{
			if (first) first = false; else buf.add(joinChr);
			buf.add(fn(a));
		}
		
		return this;
	}
	
	public function addIterJoin<T>(arr:Array<T>, fn:T->StringBuilder->Void, joinChr:String):StringBuilder 
	{
		var first = true;
		for (a in arr)
		{
			if (first) first = false; else buf.add(joinChr);
			fn(a, this);
		}
		
		return this;
	}
	
	public function toString():String 
	{
		return buf.toString();
	}
	
}